package com.cn.lycss.service.impl;

import com.cn.lycss.dao.CompanyDao;
import com.cn.lycss.dao.NavDao;
import com.cn.lycss.model.CompanyModel;
import com.cn.lycss.model.NavModel;
import com.cn.lycss.service.ICompanyService;
import com.cn.lycss.util.IOTool;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ICompanyServiceImp implements ICompanyService {
    String path = "D:\\spring-5.3.2-dist\\wanxi\\step3\\xueyuan03\\web\\note";
    String fileName = "company.txt";
    @Override
    public CompanyModel getCompanyModel() {
       IOTool.createFile(path,fileName);
//写入------------------------------------------------------
        List<String> data=  setCompanyData();
        String res=IOTool.writeData(data);
        if(res.equals("error")){
            return null;
        }
        String string=IOTool.readData();
       CompanyModel companyModel= setData(string);
        return companyModel;
    }

    private List<String> setCompanyData() {
        CompanyDao dao=new CompanyDao();
        List<String> data=new ArrayList<>();
        data.add(dao.font);
        data.add(dao.logo);
        data.add(dao.net);
        data.add(dao.phone);
        data.add(dao.warn);
        return data;
    }

    private CompanyModel setData(String string) {
        String[] split=string.split(IOTool.SPLIT);
        CompanyDao dao=new CompanyDao();
        for (int i = 0; i <split.length ; i++) {
            System.out.println("=================="+split[i]);
        }
        CompanyModel companyModel =new CompanyModel();
        companyModel.setFont(split[0]);
        companyModel.setNet(split[2]);
        companyModel.setLogo(split[1]);
        companyModel.setWarn(split[4]);
        companyModel.setPhone(split[3]);

        return companyModel;
    }




//    private String writeData(File note) {
//        if (note == null || !note.exists()) {
//            return "error";
//        }
//        try {
//            OutputStream outputStream = new FileOutputStream(note);
//            CompanyDao dao =new CompanyDao();
//            String split ="\n";
//            outputStream.write(dao.font.getBytes());
//            outputStream.write("\n".getBytes());
//            outputStream.write(dao.logo.getBytes());
//            outputStream.write("\n".getBytes());
//            outputStream.write(dao.net.getBytes());
//            outputStream.write("\n".getBytes());
//            outputStream.write(dao.phone.getBytes());
//            outputStream.write("\n".getBytes());
//            outputStream.write(dao.warn.getBytes());
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "seccess";
//    }

    public static void main(String[] args) {
        new ICompanyServiceImp().getCompanyModel();
    }
}
